package com.lwlk.zdk.controller.Assess;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lwlk.zdk.mapper.T.TDisEnterpriseMapper;
import com.lwlk.zdk.mapper.T.TDisPlatformMapper;
import com.lwlk.zdk.mapper.T.TDisVehicleMapper;
import com.lwlk.zdk.mapper.pw.pwAssessGradeMapper;
import com.lwlk.zdk.mapper.pw.pwVehicleMapper;
import com.lwlk.zdk.mapper.sys.sysEnterpriseMapper;
import com.lwlk.zdk.mapper.task.pwTaskEnterpriseAssessmentStatisticsMapper;
import com.lwlk.zdk.service.User.SysUserService;
import com.lwlk.zdk.util.HttpClientUtils;
import com.lwlk.zdk.util.PropUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.NumberFormat;
import java.util.*;

@Api(value = "企业统计信息",tags = {"企业统计信息"})
@RestController
@Transactional
@RequestMapping(value = "/EnterpriseAssess")
public class EnterpriseAssessController {


    private final static String port = PropUtils.getString("ports");
    private final static String Ip = PropUtils.getString("Ip");
    private final static String clientCode = PropUtils.getString("clientCode");

    @Autowired
    pwAssessGradeMapper pwAssessGradeMapper;
    @Autowired
    TDisEnterpriseMapper tDisEnterpriseMapper;
    @Autowired
    TDisPlatformMapper tDisPlatformMapper;
    @Autowired
    TDisVehicleMapper tDisVehicleMapper;
    @Autowired
    pwTaskEnterpriseAssessmentStatisticsMapper ptesMapper;


    @ApiOperation(value = "企业综合情况统计(查询昨天所有企业的上线率，入网率等信息)")
    @RequestMapping(value = "/comprehensiveStatistic",method = RequestMethod.GET)
    public JSONObject comprehensiveStatistic(String eIds,String limit,String page,String admdivcode){
        try {

            NumberFormat nf = java.text.NumberFormat.getPercentInstance();
            Map map=new HashMap();
            map.put("eIds",eIds);//企业id，多个id英文逗号分隔
            map.put("limit",limit);//页面记录数，默认值为10
            map.put("page",page);//页码,默认值为1
            map.put("admdivcode",admdivcode);//必传 用户行政区划代码
            Set<String> set = map.keySet();
            Iterator<String> it = set.iterator();
            while (it.hasNext()) {
                String str = it.next();
                if(map.get(str)==null || map.get(str) =="null"|| map.get(str)==""){
                    map.remove(str);
                    set = map.keySet();
                    it = set.iterator();
                }
            }
            Map<String,String>headMap=new HashMap<>();
            headMap.put("clientCode",clientCode);
            String url="http://"+Ip+":"+port+"/dis/assessment/enterprise/comprehensiveStatistic";
            String result = HttpClientUtils.doGet(url,headMap,map);
            JSONObject json= JSONObject.parseObject(result);
            JSONObject jo=json.getJSONObject("result");
            JSONArray jsonArray=jo.getJSONArray("list");
            if (jsonArray==null){
                return json;
            }
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject=jsonArray.getJSONObject(i);
                String onlineRate=jsonObject.getString("onlineRate");//上线率
                double c=Double.valueOf(onlineRate);
                String onlineRates=nf.format(c);//将上线率转成百分数
                Integer eId=Integer.parseInt(jsonObject.getString("eId"));//车辆id
                //String enterpriseName=sysEnterpriseMapper.selectByEid(eId);//企业名称
                String enterpriseName=tDisEnterpriseMapper.selectByEid(eId);
                jsonObject.put("onlineRate",onlineRates);
                jsonObject.put("enterpriseName",enterpriseName);

            }
            return json;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }



}
